package jinyilw.tools.net;

import jinyilw.common.SettingTools;
import jinyilw.common.TextTools;
import jinyilw.common.app.AppConfig;

import java.io.File;

public class NetConfig
{
	public static String netSettingsFileName = "Net.ini";
	public static boolean isServer = false;
	public static String tcpServerIP = "localhost";
	public static int tcpServerPort = 11137;
	public static int fileServerPort = 11100;
	public static String updateServerIP = "24.159.80.196";
	public static int updateServerPort = 11000;

	private static volatile SettingTools netSettings;
	private static String replaceIP;

	private static TCPParameter defaultTCPParameter;
	private static TCPParameter updateTCPParameter;

	private NetConfig()
	{
	}

	public static void set(String netSettingsFileName, boolean isServer, String tcpServerIP,
			int tcpServerPort, int fileServerPort)
	{
		NetConfig.netSettingsFileName = netSettingsFileName;
		NetConfig.isServer = isServer;
		NetConfig.tcpServerIP = tcpServerIP;
		NetConfig.tcpServerPort = tcpServerPort;
		NetConfig.fileServerPort = fileServerPort;
	}

	public static void set(String updateServerIP, int updateServerPort)
	{
		NetConfig.updateServerIP = updateServerIP;
		NetConfig.updateServerPort = updateServerPort;
	}

	private synchronized static void initSettings()
	{
		if (netSettings == null)
		{
			File settingsFile = new File(AppConfig.getSettingsFolder(),
					netSettingsFileName);
			netSettings = SettingTools.getTools(settingsFile);

			isServer = netSettings.getBool("isServer", isServer);
			tcpServerIP = netSettings.getString("tcpServerIP", tcpServerIP);
			if (replaceIP != null)
				tcpServerIP = replaceIP;
			tcpServerPort = netSettings.getInt("tcpServerPort",
					tcpServerPort);
			fileServerPort = netSettings.getInt("fileServerPort",
					fileServerPort);
			updateServerIP = netSettings.getString("updateServerIP", updateServerIP);
			updateServerPort = netSettings.getInt("updateServerPort", updateServerPort);

			netSettings.putData("tcpServerIP", tcpServerIP);
			netSettings.putData("tcpServerPort", tcpServerPort);
			netSettings.putData("fileServerPort", fileServerPort);
			netSettings.putData("isServer", isServer);
			netSettings.putData("updateServerIP", updateServerIP);
			netSettings.putData("updateServerPort", updateServerPort);
		}
	}

	public static String getTcpServerIP()
	{
		initSettings();
		return tcpServerIP;
	}

	public static int getTcpServerPort()
	{
		initSettings();
		return tcpServerPort;
	}
	public static String getUpdateServerIP()
	{
		initSettings();
		return updateServerIP;
	}
	public static int getUpdateServerPort()
	{
		initSettings();
		return updateServerPort;
	}
	public static boolean isServer()
	{
		initSettings();
		return isServer;
	}

	public static boolean isClient()
	{
		initSettings();
		return !isServer;
	}

	public static TCPParameter getDefaultTCPParameter()
	{
		initSettings();
		if (defaultTCPParameter == null)
			defaultTCPParameter = new TCPParameter(tcpServerIP, tcpServerPort);
		return defaultTCPParameter;
	}
	public static TCPParameter getUpdateTCPParameter()
	{
		initSettings();
		if (updateTCPParameter == null)
			updateTCPParameter = new TCPParameter(updateServerIP, updateServerPort);
		return updateTCPParameter;
	}
	public static boolean setServerIP(String newIP)
	{
		if (TextTools.isBlank(newIP))
			return false;
		replaceIP = newIP;
		tcpServerIP = newIP;
		if (netSettings != null)
			netSettings.putData("serverIP", tcpServerIP);
		if (defaultTCPParameter != null)
			defaultTCPParameter.serverIP = tcpServerIP;
		return true;
	}
	public static void setTcpServerPort(int port)
	{
		tcpServerPort = port;
		if (netSettings != null)
			netSettings.putData("tcpServerPort", port);
		if (defaultTCPParameter != null)
			defaultTCPParameter.port = port;
	}
}
